package com.guodu.service.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.ContractDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.Contract;
import com.guodu.entity.ContractStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.ContractService;
import com.guodu.vo.ContractQueryVo;
import com.guodu.vo.ContractVo;

@Service("contractService")
public class ContractServiceImpl implements ContractService {

	@Resource
	private ContractDao contractDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<ContractVo> findByVo(ContractQueryVo contractQueryVo,Units units,UserInfo userInfo,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<Contract> list = contractDao.findByVo(contractQueryVo,units,userInfo, curPage, pageSize);
		List<ContractVo> contractVos = new ArrayList<ContractVo>();
		for(Contract contract : list ){
			ContractVo contractVo = new ContractVo(contract);
			UserInfo createUser = userInfoDao.findById(contract.getCreatePeople());
			if(createUser.getUnitsId()!=null){
				Units createUnits = unitsDao.findById(createUser.getUnitsId());
				contractVo.setUnitsLevel(createUnits.getUnitsLevel().toString());
			}
			if(contract.getUnitsId()!=null){
			Units unitsInfo = unitsDao.findById(contract.getUnitsId());
			if(unitsInfo!=null){
			if(unitsInfo.getUnitsLevel()==1){
				contractVo.setFunitsName(unitsInfo.getUnitsName());
			}else{
				contractVo.setUnitsName(unitsInfo.getUnitsName());
				contractVo.setIndustry(unitsInfo.getIndustry());
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					contractVo.setFunitsName(funitsInfo.getUnitsName());
				}
			}
			}
			}
			contractVo.setUserName(createUser.getUserName());
			contractVos.add(contractVo);
		}
		return contractVos;
	}

	@Override
	public int findSizeByVo(ContractQueryVo contractQueryVo,Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return contractDao.findSizeByVo(contractQueryVo,units,userInfo);
	}

	@Override
	public Contract findById(String id) {
		// TODO Auto-generated method stub
		return contractDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		contractDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		contractDao.delByIds(ids);
	}

	@Override
	public void save(Contract entity) {
		// TODO Auto-generated method stub
		contractDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<ContractStatus> entities = new ArrayList<ContractStatus>();
		for(int i=0;i<idses.length;i++){
			ContractStatus entity = new ContractStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setCreateTime(Calendar.getInstance().getTime());
			entity.setStatusId(i+"");
			entities.add(entity);
		}
		contractDao.changeStatus(entities);
		contractDao.examine(ids,afterStatus);
	}

	@Override
	public void update(Contract entity) {
		// TODO Auto-generated method stub
		contractDao.update(entity);
	}

	@Override
	public List<ContractStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return contractDao.findStatus(id);
	}

	@Override
	public List<Contract> findAllByVo(ContractQueryVo contractQueryVo,
			Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return contractDao.findAllByVo(contractQueryVo, units,userInfo);
	}

	@Override
	public void examine(ContractQueryVo contractQueryVo, Units units,
			UserInfo userInfo) {
		// TODO Auto-generated method stub
		contractDao.examin(contractQueryVo, units, userInfo);
	}

	@Override
	public ContractVo findAllMoney(ContractQueryVo contractQueryVo,
			Units units, UserInfo userInfo) {
		
		return contractDao.findAllMoney(contractQueryVo,units,userInfo);
	}



}
